
package com.ami.iusb.protocol;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import com.ami.iusb.RedirProtocolException;
import com.ami.iusb.RedirectionException;
import com.ami.kvm.imageredir.IUSBHeader;
import com.ami.kvm.jviewer.Debug;

public final class IUSBSCSI extends RedirPacket {
	public byte Lba;
	public int instanceNum;
	public int connectionStatus = -1;

	public int dataLen;

	public int opcode;

	public ByteBuffer data;

	public String m_otherIP;

	private boolean preBuffered;
	public static final int IUSB_SCSI_PKT_SIZE = 62;
	public static final int IUSB_SCSI_PKT_SIZE_WITHOUT_HEADER = 30;
	public static final int IUSB_SCSI_OPCODE_INDEX = 41;
	public static final int OPCODE_EJECT = 27;
	public static final int OPCODE_KILL_REDIR = 246;
	public static final int MEDIA_SESSION_DISCONNECT = 247;
	public static final int IUSB_SCSI_OPCODE_KEEP_ALIVE = 243;
	public static final int IUSB_SCSI_OPCODE_GET_NOTIF_STAT = 74;
	public static final int IUSB_SCSI_OPCODE_GET_NOTIF_STAT_PKT_SIZE = 37;

	public IUSBSCSI(IUSBHeader paramIUSBHeader) {
		this.header = paramIUSBHeader;
		this.instanceNum = paramIUSBHeader.getInstance();
		this.dataLen = ((int) paramIUSBHeader.getDataPktLen());
	}

	public IUSBSCSI(ByteBuffer paramByteBuffer, boolean paramBoolean) throws RedirProtocolException {
		this.preBuffered = paramBoolean;
		this.header = new IUSBHeader();
		this.header.read(paramByteBuffer);
		((IUSBHeader) this.header).setDataPktLen(paramByteBuffer.limit() - this.header.headerLen);
		this.dataLen = ((int) ((IUSBHeader) this.header).getDataPktLen());
		this.data = paramByteBuffer.slice();
	}

	public void writePacket(ByteBuffer paramByteBuffer) {
		((IUSBHeader) this.header).setDirection(128);

		this.header.write(paramByteBuffer);

		if (this.preBuffered) {
			paramByteBuffer.limit(this.dataLen + 32);
			paramByteBuffer.position(paramByteBuffer.limit());
		} else {
			paramByteBuffer.put(this.data);
		}
	}

	public void readData(ByteBuffer paramByteBuffer) throws BufferUnderflowException {
		byte[] arrayOfByte = new byte[39];
		this.data = ByteBuffer.allocate(this.dataLen);

		if (paramByteBuffer.remaining() < this.dataLen) {
			throw new BufferUnderflowException();
		}
		this.data.put(paramByteBuffer);
		this.Lba = this.data.get(13);
		this.opcode = (this.data.get(9) & 0xFF);
		if ((this.opcode == 241) && (this.dataLen > 30)) {
			this.connectionStatus = this.data.get(30);
			try {
				this.data.position(31);
				this.data.get(arrayOfByte);
				this.m_otherIP = new String(arrayOfByte).trim();
			} catch (BufferUnderflowException localBufferUnderflowException) {
				System.err.println(localBufferUnderflowException.getMessage());
				this.m_otherIP = new String("");
				throw new BufferUnderflowException();
			}
		} else {
			this.connectionStatus = -1;
		}
	}

	public static void sendCommandToMediaServer(PacketMaster paramPacketMaster, ByteBuffer paramByteBuffer,
			byte[] paramArrayOfByte, int paramInt) throws RedirectionException, IOException {
		int i = 30;
		int j = i;

		paramByteBuffer.clear();
		if ((paramArrayOfByte != null) && (paramArrayOfByte.length >= 0)) {
			if (paramArrayOfByte.length > paramByteBuffer.capacity() - 32) {
				Debug.out.println("Passed buffer length is greater than allocated bytebuffer capacity");
				return;
			}
			j += paramArrayOfByte.length;
			paramByteBuffer.limit(62 + paramArrayOfByte.length);
		} else {
			paramByteBuffer.limit(62);
		}

		if ((paramPacketMaster == null) || (paramByteBuffer == null)) {
			Debug.out.println("Error sending packet. PacketMaster/packetWriteBuffer is null!!!");
			return;
		}

		IUSBHeader localIUSBHeader = new IUSBHeader(j);
		localIUSBHeader.write(paramByteBuffer);
		paramByteBuffer.position(41);
		paramByteBuffer.put((byte) (paramInt & 0xFF));
		if (paramArrayOfByte != null) {
			paramByteBuffer.position(62);
			paramByteBuffer.put(paramArrayOfByte);
		}
		paramByteBuffer.position(0);
		IUSBSCSI localIUSBSCSI = new IUSBSCSI(paramByteBuffer, true);

		paramPacketMaster.sendPacket(localIUSBSCSI);
	}
}
